﻿#ifndef SQLITEDB_H
#define SQLITEDB_H

/**************************
 *sql操作工具 *
 ***************************/


#include <QDir>
#include <QDate>
#include <QDateTime>
#include <QFileInfo>
#include <QString>
#include <QTime>
#include <QSqlDatabase>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QVariant>
#include <QSqlTableModel>

typedef QList<QMap<QVariant,QVariant>> DB_TYPE_LIST;
typedef QMap<QVariant,QVariant> DB_TYPE_DATA;

class SqliteDb
{
public:
	SqliteDb();
	~SqliteDb();
public:
    bool insertRow(const QString &tableName, const QMap<QVariant, QVariant> &dataMap);
    bool getRow(const QString &tableName, DB_TYPE_DATA &data, const DB_TYPE_DATA &filterMap=DB_TYPE_DATA());
    bool getRows(const QString &tableName, DB_TYPE_LIST &data, const DB_TYPE_DATA &filterMap=DB_TYPE_DATA());
    bool getRows(const QString &tableName, DB_TYPE_LIST &data, const QString &strWhere);
    bool delRow(const QString &tableName, const DB_TYPE_DATA&filterMap=DB_TYPE_DATA());
    bool updateRow(const QString &tableName, const DB_TYPE_DATA &dataMap, const DB_TYPE_DATA&filterMap=DB_TYPE_DATA());
    bool openTransation(){return transaction();}
    bool commit();
    bool reOpenSql(const QString &dbName);//打开连接
    int getMaxId(const QString &tableName);
public:
    static SqliteDb m_SqliteDb;
    static QString m_dbName;
    static QSqlDatabase m_sqlDb;
private:
	bool setDbDir(QString dirPath);//设置数据库存放路径
	bool creatDbFile(QString dbName);//生成一个db文件

	QSqlDatabase getOpenSql(QString dbName);
	bool closeSql();//关闭连接
	bool queryExec(QString dbName,QString sqlStr);//执行sql语句，不获取结果
    bool queryExec(QString dbName,QString sqlStr,QList<QMap<QVariant,QVariant>> &data);//执行sql语句，并获取结果
    bool getData(QString dbName,QString tableName,QMap<QVariant,QVariant> &data,QString sqlWhere=""); //获取数据
    bool getData(QString dbName,QString table,QList<QMap<QVariant,QVariant>> &data,QString sqlWhere=""); //获取数据
    bool getData(QString dbName,QString tableName,QMap<QVariant,QVariant> columndata,QList<QMap<QVariant,QVariant>> &data,QString sqlWhere=""); //获取数据
    bool addData(QString dbName,QString tableName,const QMap<QVariant,QVariant> &data);//增加
	bool delData(QString dbName,QString tableName,QString sqlWhere);//删除
    bool updateData(QString dbName,QString tableName,QMap<QVariant,QVariant> data,QString sqlWhere="");//修改

	bool queryExec(QString sqlStr);//执行sql语句，不获取结果
    bool queryExec(QString sqlStr,QList<QMap<QVariant,QVariant>> &data);//执行sql语句，并获取结果
    bool getData(QString tableName,QMap<QVariant,QVariant> &data,QString sqlWhere=""); //获取数据
    bool getData(QString table,QList<QMap<QVariant,QVariant>> &data,QString sqlWhere=""); //获取数据
    bool getData(QString tableName,QMap<QVariant,QVariant> columndata,QList<QMap<QVariant,QVariant>> &data,QString sqlWhere=""); //获取数据
	bool delData(QString tableName,QString sqlWhere);//删除
    bool updateData(QString tableName,QMap<QVariant,QVariant> data,QString sqlWhere="");//修改
	bool transaction();
    QString getErrorSql();//获取错误的数据库语句
private:
	QString dbFilePath;//数据库路径
	QString dbDir; //数据库文件夹
	QString databaseName;//数据库名字
	QString errorSqlText;//错误语句
private:
	void errorSql(QString sql);//错误打印
    QString getWhereStr(const DB_TYPE_DATA &filterMap);
};

#endif // SQLITEDB_H

